/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Common;

/**
 * The class wich represents messages used in this application.
 * @author hartingm
 */
public class Message {

    /********************
    * Public Attributes
     ********************/

	/**
	 * Used to signal to the server that a client wants to be connected.
	 */
	public static final byte CONNECTION = 1;

    /**
     * Used by server to confirm connection of a client.
     */
    public static final byte CONNECTION_OK = 2;

	/**
	 * Used by server or clients to signal a disconnection.
	 */
	public static final byte DISCONNECTION = 3;
	
	/**
	 * Used by clients to send private messages.
	 */
	public static final byte PRIVATE_MESSAGE = 4;

	/**
	 * Used by server and clients to send message on public channel.
	 */
	public static final byte PUBLIC_MESSAGE = 5;
	
	/**
	 * Used by server to order to a client to add a new entry his list of connected client.
	 */
	public static final byte ADD_CLIENT = 6;

	/**
	 * Used by server to order to a client to delete a new entry his list of connected client.
	 */
	public static final byte DELETE_CLIENT = 7;
	
	/**
	 * Used by clients to propose a file transfert to an other client.
	 */
	public static final byte FILE_PROPOSE = 8;

	/**
	 * Used by client to answer to a file tranfert proposition positively.
	 */
	public static final byte FILE_ACCEPT = 9;

	/**
	 * Used by client to answer to a file tranfert proposition negatively.
	 */
	public static final byte FILE_DENY = 10;

	/**
	 * Used by client during a file tranfert session. This type of message contains file pieces.
	 */
	public static final byte FILE = 11;

	/**
	 * Used by clients during a file tranfert to signal that the file is totaly sent.
	 */
	public static final byte FILE_END = 12;

	/**
	 * Used by client to kick an other client.
	 */
	public static final byte KICK_MESSAGE = 13;
    

    /********************
    * Private Attributes
     ********************/
	//message type
    private byte	type;
	
    //nickname source
    private String  nickSrc;
    
    //nickname destination
    private String  nickDest;
     
    //message
    private byte[]  message;

	//lenght (only used in FILE messages)
	private int lenght;
    

	/**
	 *Build a new message.
	 *Message type may be: 1-connection, 2-disconnection, 3-message, 4-addClient, 5-deleteClient.
	 * 
	 *@param	type		message type
	 *@param	nickSrc		message sender
	 *@param	nickDest	message recipient (may be empty)
	 *@param	message		the message (may be empty) as a String
	 */
    public Message(byte type, String nickSrc, String nickDest, String message){
        this.type = type;
        this.nickSrc = nickSrc;
        this.nickDest = nickDest;
		this.lenght = 0;
        this.message = message.getBytes();
    }

	/**
	 * Build a new message. This constructor is used to transfert file.
	 * Message type may be: 1-connection, 2-disconnection, 3-message, 4-addClient, 5-deleteClient.
	 *
	 *@param	type		message type
	 *@param	nickSrc		message sender
	 *@param	nickDest	message recipient (may be empty)
	 *@param    lenght		the length of the byte[] message
	 *@param	message		the message (may be empty) as a byte[]
	 */
	public Message(byte type, String nickSrc, String nickDest, int lenght, byte[] message) {
		this.type = type;
		this.nickSrc = nickSrc;
		this.nickDest = nickDest;
		this.message = message;
		this.lenght = lenght;
	}

	/**
	 * Return the message type
	 * @return message type
	 */
    public byte getType() {
        return this.type;
    }

	/**
	 * Return the message sender
	 * @return message sender
	 */
    public String getNickSrc() {
        return this.nickSrc;
    }

	/**
	 * Return the message recipient
	 * @return message recipient
	 */
    public String getNickDest() {
        return this.nickDest;
    }

	/**
	 * Return the message as a String
	 * @return message the message as a String
	 */
    public String getMessage() {
        return new String(message);
    }

	/**
	 * Return the message as a byte[]
	 * @return message the message as a byte[]
	 */
	public byte[] getMessageBytes() {
		return message;
	}

	/**
	 * Return the length of the byte array in the message.
	 * @return the length of the byte array in the message
	 */
	public int getLength() {
		return lenght;
	}

}//end Message